package br.com.documentos.model.util;

import java.security.Security;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailUtil {

	private String usuario;
	private String senha;
	private String remetente;
	
	private static final String SMTP_HOST_NAME = "smtp.gmail.com";
	private static final String SMTP_PORT = "465";
	private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

	public EmailUtil(String usuario, String senha, String remetente){
		this.usuario = usuario;
		this.senha = senha;
		this.remetente = remetente;
	}
		
	public void enviar(String titulo, String conteudo, String ... destinatarios) throws MessagingException {
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

		Properties props = new Properties();
		props.put("mail.smtp.host", SMTP_HOST_NAME);
		props.put("mail.smtp.auth", "true");
		props.put("mail.debug", "true");
		props.put("mail.smtp.port", SMTP_PORT);
		props.put("mail.smtp.socketFactory.port", SMTP_PORT);
		props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
		props.put("mail.smtp.socketFactory.fallback", "false");

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(usuario,senha);
					}
				});
		session.setDebug(false);

		Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(remetente));

		InternetAddress[] addressTo = new InternetAddress[destinatarios.length];
		for (int i = 0; i < destinatarios.length; i++) {
			addressTo[i] = new InternetAddress(destinatarios[i]);
		}
		msg.setRecipients(Message.RecipientType.TO, addressTo);
		
		msg.setSubject(titulo);
		msg.setContent(conteudo, "text/html; charset=UTF-8");
		Transport.send(msg);
		
		
		
//		
//	      Properties props = new Properties();
//	      props.setProperty("mail.transport.protocol", "smtp");
//	      props.setProperty("mail.host", "mymail.server.org");
//	      props.setProperty("mail.user", "emailuser");
//	      props.setProperty("mail.password", "");
//
//	      Session mailSession = Session.getDefaultInstance(props, null);
//	      Transport transport = mailSession.getTransport();
//
//	      MimeMessage message = new MimeMessage(mailSession);
//	      message.setSubject("Testing javamail html");
//	      message.setContent
//	         ("This is a test <b>HOWTO<b>", "text/html; charset=ISO-8859-1");
//	      message.addRecipient(Message.RecipientType.TO,
//	         new InternetAddress("elvis@presley.org"));
//
//	      transport.connect();
//	      transport.sendMessage(message,
//	         message.getRecipients(Message.RecipientType.TO));
//	      transport.close();		
		
		
		
	}
}
